package com.kmust.dream.wanderingdream.upload.controller;

import com.kmust.dream.wanderingdream.upload.dto.ImageDTO;
import com.kmust.dream.wanderingdream.upload.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/upload")
@CrossOrigin
@Api("上传文件的API")
public class UploadController {


    @Autowired
    private UploadService uploadService;


    /**
     * 此方法将文件上传至阿里云的OSS存储仓库
     *
     * @param multipartFile 上传文件 key -> file
     * @return 返回已经上传的url
     */
    @ApiOperation(value = "单个上传文件，返回该文件在云端的url",notes = "参数是名为file的参数")
    @PostMapping("/uploadFile")
    public String getUrl(@RequestParam("file") MultipartFile multipartFile) {
        return uploadService.getUrl(multipartFile);
    }

    /**
     * 此方法将多文件上传至阿里云的OSS存储仓库
     *
     * @param files 文件数组
     * @return 所有的url集合
     */
    @ApiOperation(value = "多个上传文件，返回该文件在云端的url的list<String>",notes = "参数是名为files的参数")
    @PostMapping("/uploadFiles")
    public List<String> getUrls(@RequestParam("files") MultipartFile[] files) {
        return uploadService.getUrls(files);
    }

    /**
     * 重新获取大小
     *
     * @param imageDTO imageDTO对象
     * @return 新的url
     */
    @ApiOperation(value = "重新设置图片大小，返回该文件在云端的url",notes = "参数是名为ImageDTO的json数据")
    @GetMapping("/resize")
    public String resizeImage(@RequestBody ImageDTO imageDTO) {
        return uploadService.resizeImage(imageDTO);
    }




}
